package cn.pli.bike.ui;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.ImageView;
import android.widget.TextView;

import com.framemodule.base.BaseDelegate;
import com.framemodule.base.ImageLoaderHelper;
import com.framemodule.utils.CommonUtils;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.tencent.connect.UserInfo;

import java.util.ArrayList;
import java.util.List;

import cn.pli.bike.R;
import cn.pli.bike.bean.UserBean;
import cn.pli.bike.bean.UserInfoSetBean;
import cn.pli.bike.utils.AppUtils;

/**
 * Created by shangdongzhou on 2017/11/8.
 */

public class UserInfoDetailDelegate extends BaseDelegate {

    private ImageView ivUserphoto;
    private RecyclerView recyclerView;
    private UserInfoAdapter userInfoAdapter;
    private static int[] titles = {R.string.real_name_auth, R.string.phone_number, R.string.regist_time};
    private List<UserInfoSetBean> userInfos;
    private TextView tvTitle;


    @Override
    public void initTitleBar() {

    }

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_user_detail_info;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        tvTitle = get(R.id.tv_title);
        tvTitle.setText("个人信息");
        ivUserphoto = get(R.id.iv_user_photo);
        recyclerView = get(R.id.recycler_user_info);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
        userInfoAdapter = new UserInfoAdapter(recyclerView);
        recyclerView.setAdapter(userInfoAdapter);
        userInfos = new ArrayList<>();
        setData();

    }

    public void setData() {
        userInfos.clear();
        for (int i = 0; i < titles.length; i++) {
            userInfos.add(new UserInfoSetBean(getActivity().getString(titles[i])));
        }
        userInfoAdapter.setData(userInfos);
    }


    public UserInfoAdapter getUserInfoAdapter() {
        return userInfoAdapter;
    }

    public void setUserInfoAdapter(UserInfoAdapter userInfoAdapter) {
        this.userInfoAdapter = userInfoAdapter;
    }

    public void updateUserInfo(UserBean userBean) {
        ImageLoader.getInstance().displayImage(userBean.getPhoto(), ivUserphoto, ImageLoaderHelper.getInstance(this.getActivity()).getDisplayAddHeader());
        boolean isAuth = AppUtils.isAuthStatus();
        if (!isAuth) {

            userInfos.get(0).setDescribe(getActivity().getString(R.string.no_auth));
            userInfos.get(0).setColor(Color.RED);
        } else {


            userInfos.get(0).setDescribe(getActivity().getString(R.string.has_auth));
            userInfos.get(0).setColor(Color.GRAY);
        }

        String phone = userBean.getMobile();
        if (CommonUtils.isNotEmpty(phone) && phone.length() == 11) {
            userInfos.get(1).setDescribe(phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
        }

        userInfos.get(2).setDescribe(CommonUtils.parseDate(userBean.getRegisterTime()));
        for (UserInfoSetBean userInfo : userInfos) {
            userInfo.setHideNext(true);
        }

        userInfoAdapter.notifyDataSetChanged();
    }

    public ImageView getIvUserphoto() {
        return ivUserphoto;
    }

    public RecyclerView getRecyclerView() {
        return recyclerView;
    }
}
